#include "qs_config.h"
#include "qs_log.h"
#include "qs_action.h"
#include "qs_sched.h"
#include "qs_rf_protocol.h"

uint8_t aTxBuffer[Packet_Length] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
uint8_t aRxBuffer[Packet_Length] = {0x05, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};

typedef struct {
    qs_rf_work_mode_t mode;
    uint16_t delay_cnt;
}qs_work_handle_t;
static qs_work_handle_t m_work_handle;

static uint8_t m_tx_ack_timeout_timer = 0;
static qs_rf_protocol_on_tx_ready_handle_s m_on_tx_ready = NULL;
static qs_rf_protocol_on_tx_payload_ack_handle_s m_on_tx_payload_ack = NULL;
static qs_rf_protocol_on_rx_data_handle_s m_on_rx_data = NULL;

static qs_rf_protocol_t m_tx_frame;
static void rf_protocol_work_handle_set(qs_rf_work_mode_t mode, uint16_t delay_cnt)
{
    m_work_handle.mode = mode;
    m_work_handle.delay_cnt = delay_cnt;
}

void rf_protocol_tx_mode_data_send(qs_rf_protocol_t* p_frame)
{
    unsigned char cnt,buf;
    memcpy(aTxBuffer, p_frame->daten, Packet_Length - 1);
          
    buf=aTxBuffer[0];
    for(cnt=1;cnt<Packet_Length-1;cnt++)
        buf^=aTxBuffer[cnt];
				
    aTxBuffer[Packet_Length-1]=buf;
		
    LT8960L_TxData(Test_Channel,aTxBuffer,Packet_Length);
    rf_protocol_work_handle_set(RF_TX_ACK_SET_MODE, 0);
}

static void rf_protocol_tx_mode(void)
{
    RXBusy=0;
    if(m_on_tx_ready) {
        m_on_tx_ready();
    }
}
static void rf_protocol_tx_ack_set_mode(void)
{
    LT8960L_WriteReg(7,0,Test_Channel);
				
#ifdef Air_rate_62K5	
    LT8960L_WriteReg( 8, 0x6c, 0x50);
#else
    LT8960L_WriteReg( 8, 0x6c, 0x90);
#endif
    LT8960L_WriteReg(52, 0x80, 0x80);
    LT8960L_WriteReg(7,0,Test_Channel|0X80); 
    rf_protocol_work_handle_set(RF_TX_ACK_MODE, 0);
    m_tx_ack_timeout_timer = 20;
}
static void rf_protocol_tx_ack_mode(void)
{
    unsigned char cnt,buf;
    if(RXBusy==0) {
        RXBusy=1;
        LT8960L_WriteReg(7,0,Test_Channel);

#ifdef Air_rate_62K5	
        LT8960L_WriteReg( 8, 0x6c, 0x50);
#else
        LT8960L_WriteReg( 8, 0x6c, 0x90);
#endif

        LT8960L_WriteReg(52, 0x80, 0x80);
        //LT8960L_WriteReg(50, 0, 0);
        LT8960L_WriteReg(7,0,Test_Channel|0X80); 
						
	return;
    }   
				
    if(LT8960L_GetPKT() && RXBusy) {
        RXBusy=0; 
								
        LT8960L_ReadReg(48);
        if((LT8960L_RegH&0x80)==0) {
        LT8960L_ReadBUF(50,aRxBuffer);

        buf=aRxBuffer[0];
        for(cnt=1;cnt<Packet_Length-1;cnt++)
            buf^=aRxBuffer[cnt];
						
        if(buf==aRxBuffer[Packet_Length-1]) {
            T1S_Count++;				  
           // LED_CPL;
            if(m_on_tx_payload_ack) {
                m_on_tx_payload_ack((qs_rf_protocol_t*)aRxBuffer);
            }
            
            rf_protocol_work_handle_set(RF_TX_MODE, m_tx_ack_timeout_timer);
            m_tx_ack_timeout_timer = 0;
            }
            RXBusy=0; 
        }
        RXBusy=0; 
    }
    if(m_tx_ack_timeout_timer == 0) {
        rf_protocol_work_handle_set(RF_TX_MODE, 0);
    }
}

void rf_protocol_tx_ack_payload_data_send(qs_rf_protocol_t* p_frame)
{
    unsigned char cnt,buf;
    memcpy(aTxBuffer, p_frame->daten, Packet_Length - 1);
          
    buf=aTxBuffer[0];
    for(cnt=1;cnt<Packet_Length-1;cnt++)
        buf^=aTxBuffer[cnt];
				
    aTxBuffer[Packet_Length-1]=buf;
}


static void rf_protocol_rx_mode(void)
{
    static unsigned char cnt,buf;
    if(LT8960L_GetPKT() && RXBusy) {
        RXBusy=0; 
        LT8960L_ReadReg(48);
        
        if((LT8960L_RegH&0x80)==0) {
            LT8960L_ReadBUF(50,aRxBuffer);
            RXBusy=0;  
            buf=aRxBuffer[0];
            for(cnt=1;cnt<Packet_Length-1;cnt++)
					     buf^=aRxBuffer[cnt];

//                buf^=aRxBuffer[cnt];
				
            if(buf==aRxBuffer[Packet_Length-1]) {
                T1S_Count++;
	
                __NOP();
               // LED_CPL; 
                if(m_on_rx_data) {
                    m_on_rx_data((qs_rf_protocol_t*)aRxBuffer);
                }

//								if(provision_enable)
                  rf_protocol_work_handle_set(RF_RX_ACK_MODE, 5);
                return;
            }
        }
//				else 
//						  QS_LOG_INFO("crc error!\r\n");
    }
    if(RXBusy==0) {
        RXBusy=1;
        LT8960L_WriteReg(7,0,Test_Channel);
        //LT8960L_Change_0x38();

#ifdef Air_rate_62K5	
        LT8960L_WriteReg( 8, 0x6c, 0x50);
#else
        LT8960L_WriteReg( 8, 0x6c, 0x90);
#endif

        LT8960L_WriteReg(52, 0x80, 0x80);
        LT8960L_WriteReg(7,0,Test_Channel|0X80);  
					
    }
}

static void on_1ms_handle(void)
{
    if(m_work_handle.delay_cnt > 0) {
        m_work_handle.delay_cnt --;
        return;
    }
    if(m_tx_ack_timeout_timer > 0) {
        m_tx_ack_timeout_timer --;
    }
    switch(m_work_handle.mode) {
    case RF_TX_MODE:
        rf_protocol_tx_mode();
        break;
    case RF_TX_ACK_SET_MODE:
        rf_protocol_tx_ack_set_mode();
        break;
    case RF_TX_ACK_MODE:
      rf_protocol_tx_ack_mode();
        break;
    case RF_RX_MODE:
        rf_protocol_rx_mode();
        break;
    case RF_RX_ACK_MODE:
        LT8960L_TxData(Test_Channel,aTxBuffer,Packet_Length);
        rf_protocol_work_handle_set(RF_RX_MODE, 1);
		    if(provision_enable && aTxBuffer[1] == 0x83){
		      provision_enable = false;
		      m_provison_timeout = 0;
				}
        break;
    default:;
    }

}

qs_ret_code_t qs_rf_protocol_init(qs_rf_work_mode_t mode)
{
    if(mode == RF_TX_MODE) {
        QS_LOG_INFO("TX MODE\r\n");
    }else if(mode == RF_RX_MODE) {
        QS_LOG_INFO("RX MODE\r\n");
    }
    rf_protocol_work_handle_set(mode, 0);
    qs_rf_protocol_on_1ms_handle_set(on_1ms_handle);
    LT8960L_INIT();
    return QS_SUCCESS;
}
void qs_rf_protocol_tx_mode_event_handle_set(qs_rf_protocol_on_tx_ready_handle_s on_tx_ready, qs_rf_protocol_on_tx_payload_ack_handle_s on_tx_payload_ack)
{
    m_on_tx_ready = on_tx_ready;
    m_on_tx_payload_ack = on_tx_payload_ack;
}
void qs_rf_protocol_rx_mode_event_handle_set(qs_rf_protocol_on_rx_data_handle_s on_rx_data)
{
    m_on_rx_data = on_rx_data;
}
